﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Restart.Client.ServiceReference1;

namespace Restart.Client
{
    public partial class MainForm : Form
    {
        private bool _userClosing = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void Restart()
        {
            try
            {
                using (WcfServiceClient service = new WcfServiceClient())
                {
                    service.Restart(ConfigurationManager.AppSettings["Password"]);
                }
            }
            catch (Exception ex)
            {
                this.ShowErrorMessage(ex);
            }
        }

        private void Shutdown()
        {
            try
            {
                using (WcfServiceClient service = new WcfServiceClient())
                {
                    service.Shutdown(ConfigurationManager.AppSettings["Password"]);
                }
            }
            catch (Exception ex)
            {
                this.ShowErrorMessage(ex);
            }
        }

        private void ShowErrorMessage(Exception ex)
        {
            MessageBox.Show("Unable to contact the server. Make sure the service is running, that the correct address is set in configuration files on both server and client and that the firewall on the server does not block the port specified in the URI.", "RestartClient", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void restartButton_Click(object sender, EventArgs e)
        {
            this.Restart();     
        }

        private void shutdownButton_Click(object sender, EventArgs e)
        {
            this.Shutdown();
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Restart();
        }

        private void shutdownToolStripMenuItem_Click(object sender, EventArgs e)
        {            
            this.Shutdown();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._userClosing = true;
            Application.Exit();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.theNotifyIcon.Visible = true;
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)            
                this.Hide();
        }

        private void theNotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Let Windows close the application if restarting or shutdown.
            if (e.CloseReason == CloseReason.WindowsShutDown || e.CloseReason == CloseReason.TaskManagerClosing)
                return;

            // Only close the application if the user clicks the Exit menu item.
            if (this._userClosing)
            {
                this.theNotifyIcon.Visible = false;
                this.theNotifyIcon.Dispose();
            }
            else
            {
                e.Cancel = !this._userClosing;
            }

            // Hide the window, to show only the notify icon instead of closing the application.
            if (!this._userClosing) this.Hide();
        }
    }
}
